<?php

namespace IDock\Insurance\LIHI\Response;

use IDock\Http\Response;
use IDock\Insurance\LIHI\Response\AllLIHIResponse;
use IDock\Printer\preInquiryPrinter;
use IDock\Printer\UploadFilePrinter;

class UploadFileQueryResponse extends AllLIHIResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace("\\",' ',$body);
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);

        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }
        $resultMess = $data['result']['resultMessage'] ?? '';
        $status = $data['result']['status'] ?? '';

        if(!$status) {
            $this->setError($resultMess);
            return $this;
        }
        $this->setBody($data);

        return $this;
    }


    public function printerInit(): Response
    {
        $this->printer = new preInquiryPrinter();
        $body = $this->getBody()['result'] ?? [];
        $arr = $body['prpallVisaInfo']['visaInfoResult'] ?? [];
        $list = [];
        $nameArr = [];
        foreach ($arr as $value){
            if(($value['sort'] ?? '') && ($value['isUpLoad'])){
                $list[] = $value;
                $nameArr[] = $value['typeName'] ?? '';
            }
        }
        $this->printer->argc['list'] = $list;
        $this->printer->argc['nameArr'] = $nameArr;
        return $this;
    }
}
